package com.xieyingwangluo.yxpart.api;

import com.xieyingwangluo.yxpart.bean.Result;
import com.xieyingwangluo.yxpart.entity.AdminModel;
import com.xieyingwangluo.yxpart.logic.AdminLogic;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@CrossOrigin
@Api(tags = "Admin接口")
@Controller
@RequestMapping(value = "/api/v1/admin")
public class AdminBaseRest extends BaseRest {

    @Autowired
    private AdminLogic adminLogic;

    //注册
    @ApiOperation(value = "新增admin", notes = "post Rest接口的Admin方法", produces = "application/json")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "noid", value = "主键", required = false, dataType = "String", paramType = "form"),
            @ApiImplicitParam(name = "tel", value = "手机号", required = true, dataType = "String", paramType = "form"),
            @ApiImplicitParam(name = "pwd", value = "密码", required = true, dataType = "String", paramType = "form"),
            @ApiImplicitParam(name = "name", value = "姓名", required = true, dataType = "String", paramType = "form"),
            @ApiImplicitParam(name = "head_img", value = "头像", required = false, dataType = "String", paramType = "form"),
            @ApiImplicitParam(name = "power", value = "权限", required = false, dataType = "String", paramType = "form"),
            @ApiImplicitParam(name = "state", value = "状态", required = false, dataType = "String", paramType = "form"),
            @ApiImplicitParam(name = "create_time", value = "创建时间", required = false, dataType = "String", paramType = "form")
    })
    @ResponseBody
    @PostMapping(value = "/add")
    public Result add(@RequestHeader(name = "Authorization", defaultValue = "token") String token,
                      AdminModel adminModel) {

        return adminLogic.add(adminModel);
    }

    @ApiOperation(value = "更新admin", notes = "更新admin", produces = "application/json")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "noid", value = "主键", required = true, dataType = "String", paramType = "form"),
            @ApiImplicitParam(name = "tel", value = "手机号", required = false, dataType = "String", paramType = "form"),
            @ApiImplicitParam(name = "pwd", value = "密码", required = false, dataType = "String", paramType = "form"),
            @ApiImplicitParam(name = "name", value = "姓名", required = false, dataType = "String", paramType = "form"),
            @ApiImplicitParam(name = "head_img", value = "头像", required = false, dataType = "String", paramType = "form"),
            @ApiImplicitParam(name = "power", value = "权限", required = false, dataType = "String", paramType = "form"),
            @ApiImplicitParam(name = "state", value = "状态", required = false, dataType = "String", paramType = "form"),
            @ApiImplicitParam(name = "create_time", value = "创建时间", required = false, dataType = "String", paramType = "form")
    })
    @ResponseBody
    @PostMapping(value = "/upd")
    public Result update(@RequestHeader(name = "Authorization", defaultValue = "token") String token,
                         HttpSession session,
                         AdminModel adminModel) {
        return adminLogic.update(adminModel);
    }


    //查询所有
    @ApiOperation(value = "查询所有", notes = "查询所有", produces = "application/json")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "noid", value = "主键", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "tel", value = "手机号", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "pwd", value = "密码", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "姓名", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "head_img", value = "头像", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "power", value = "权限", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "state", value = "状态", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "create_time", value = "创建时间", required = false, dataType = "String", paramType = "query")})
    @ResponseBody
    @GetMapping(value = "/selA")
    public Result selA(@RequestHeader(name = "Authorization", defaultValue = "token") String token,
                       AdminModel adminModel) {
        return adminLogic.selA(adminModel);
    }

    /**
     * 删除：管理员编号(noid)
     */
    @ApiOperation(value = "删除管理员", notes = "删除管理员", produces = "application/json")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "noid", value = "管理员id", required = true, dataType = "String", paramType = "query", defaultValue = "")
    })
    @ResponseBody
    @DeleteMapping(value = "/del")
    public Result del(@RequestHeader(name = "Authorization", defaultValue = "token") String token,
                      @RequestParam("noid") String noid
    ) {
        return adminLogic.del(noid);
    }

    //管理员登录
    @ApiOperation(value = "管理员登录", notes = "管理员登录", produces = "application/json")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "用户名", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "pwd", value = "密码", required = true, dataType = "String", paramType = "query")
    })
    @ResponseBody
    @PostMapping(value = "/checklogin")
    public Result checklogin(@RequestHeader(name = "Authorization", defaultValue = "token") String token,
                             HttpSession session, @RequestParam("name") String name, @RequestParam("pwd") String pwd) {
        return adminLogic.checklogin(session, name, pwd);
    }

    //更新管理员密码
    @ApiOperation(value = "更新管理员密码", notes = "更新管理员密码", produces = "application/json")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "noid", value = "管理员id", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "pwd", value = "密码", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "oldpwd", value = "旧密码", required = true, dataType = "String", paramType = "query")
    })
    @ResponseBody
    @PostMapping(value = "/updPwd")
    public Result updatePassword(@RequestHeader(name = "Authorization", defaultValue = "token") String token,
                                 @RequestParam("noid") String noid,
                                 @RequestParam("pwd") String pwd,
                                 @RequestParam("oldpwd") String oldpwd) {
        return adminLogic.updPwd(noid, pwd, oldpwd);
    }

    //根据id获取用户信息
    @ApiOperation(value = "根据id获取用户信息", notes = "根据id获取用户信息", produces = "application/json")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "noid", value = "普通用户id", required = true, dataType = "String", paramType = "query")
    })
    @ResponseBody
    @GetMapping(value = "/selO")
    public Result selO(@RequestHeader(name = "Authorization", defaultValue = "token") String token,
                       @RequestParam("noid") String noid) {
        return adminLogic.selO(noid);
    }
}
